package gopdf

import "testing"

func TestSetProtection(t *testing.T) {

	var pp PDFProtection
	pp.setProtection(PermissionsPrint|PermissionsCopy|PermissionsModify, []byte("5555"), []byte("1234"))
	var realOValue = []byte{
		0xbb, 0xb8, 0x04, 0x6d, 0x96, 0xa9, 0x9a, 0x23, 0x46, 0xa9, 0x41, 0x21, 0x06, 0x8c, 0xad, 0x4f, 0x83, 0x5e, 0x5d, 0x0e, 0xcb, 0xb6, 0x20, 0xa8, 0xb7, 0xa3, 0x16, 0x13, 0x3c, 0x8f, 0x02, 0x91,
	}

	var realUValue = []byte{
		0x19, 0x27, 0x67, 0x2a, 0x4f, 0x28, 0x64, 0xb3, 0x8b, 0x8b, 0x40, 0x44, 0x2, 0xa2, 0x68, 0x72, 0x2f, 0xe1, 0xb9, 0xf8, 0x4, 0x24, 0x81, 0xe, 0xe8, 0x84, 0xd8, 0x30, 0xd5, 0xe9, 0x8f, 0x24,
	}

	if !isSliceEq(pp.oValue, realOValue) {
		t.Errorf("wrong oValue")
		return
	}

	if !isSliceEq(pp.uValue, realUValue) {
		t.Errorf("wrong oValue")
		return
	}

	if pp.pValue != -36 {
		t.Errorf("wrong pValue")
		return
	}

	var realObjKey4 = []byte{
		0xb3, 0x9, 0xe6, 0x55, 0xd8, 0x23, 0xbf, 0xbb, 0xc5, 0xdf,
	}
	if !isSliceEq(pp.objectkey(4), realObjKey4) {
		t.Errorf("wrong objectkey 4")
		return
	}

	var realObjKey5 = []byte{
		0xc4, 0x2c, 0x3e, 0x35, 0x92, 0xbe, 0x5e, 0x25, 0xdd, 0x1b,
	}
	if !isSliceEq(pp.objectkey(5), realObjKey5) {
		t.Errorf("wrong objectkey 5")
		return
	}
}

func isSliceEq(a, b []byte) bool {

	if a == nil && b == nil {
		return true
	}

	if a == nil || b == nil {
		return false
	}

	if len(a) != len(b) {
		return false
	}

	for i := range a {
		if a[i] != b[i] {
			return false
		}
	}

	return true
}
